package com.froggushka.api.onehourtranslation;

/*
Copyright (c) 2011 Froggushka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

//todo finish writing setters for rest of the Query option
public class RequestOptions
{
  public enum ContentTypeEnum{
    PLAIN("text/plain") , XML("text/xml");
    
    private final String m_strContentType;
    
    private ContentTypeEnum(String p_strContentType)
    {
      m_strContentType = p_strContentType;
    }

    public String getContentType()
    {
      return m_strContentType;
    }
  };
  
  private enum eQueryParameter
  {    
    //Source language
    SOURCE("source")
    
    //Target language
    , TARGET("target")
    
    //set to "proofreading" for proofreading projects
    ,PROJECT_TYPE("project_type")
    
    //(string) The source text to be translated
    , SOURCE_TEXT("source_text")
    
    //(string) The text to be proofread
    , TRANSLATED_TEXT("translated_text")
    
    //(integer>0) Number of words in the source text. If missing or 0, will be calculated automatically.
    , WORD_COUNT("word_count")
    
    // Any additional notes for the translator.
    , NOTES("notes")
    
    // Format of the submitted content for translation  Default "text/plain" - for regular text, "text/xml" - for XML documentd
    , CONTENT_TYPE("content_type")
    
    //Callback URL to receive project-related notifications
    , CALLBACK_URL("callback_url")
    
    //optional. This field will be reported back on each callback request.
    , USER_REFERANCE ("user_reference")
    
    //optional. This field will be reported back on each callback request.
    , PROJECT_REFERANCE("project_reference")
    
    //optional, 1000 chars maximum. Those fields will be reported back on each callback request.
    , CUSTOM("custom");

    private final String m_strQueryParameter;

    private eQueryParameter(String p_strQueryParameter)
    {
      m_strQueryParameter = p_strQueryParameter;
    }

    public String getParameter()
    {
      return m_strQueryParameter;
    }
  };
  // End of eQueryParameter
  

  
  private List<NameValuePair> m_listOfQueryParams = new ArrayList<NameValuePair> ();
  
  public void setLanguageSource(LanguageEnum p_eSourceLanguage)
  {
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.SOURCE.getParameter(), p_eSourceLanguage.getLanguageCode()));
  }
  
  public void setLanguageTarget(LanguageEnum p_eTargetLanguage)
  {
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.TARGET.getParameter(), p_eTargetLanguage.getLanguageCode()));
  }
  
  public void setSourceText(byte[] p_byteArraySourceText, ContentTypeEnum p_eSourceTextType)
  {
  
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.SOURCE_TEXT.getParameter(),Base64.encodeBase64String(p_byteArraySourceText)));
    
    if(p_eSourceTextType != null)
    {
      m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.CONTENT_TYPE.getParameter(), p_eSourceTextType.getContentType()));
    }
  }
  
  
  public void setTranslatedText(byte[] p_byteArrayTranslatedText, ContentTypeEnum p_eTranslatedTextType)
  {
  
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.TRANSLATED_TEXT.getParameter(),Base64.encodeBase64String(p_byteArrayTranslatedText)));
    
    if(p_eTranslatedTextType != null)
    {
      m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.CONTENT_TYPE.getParameter(), p_eTranslatedTextType.getContentType()));
    }
  }
  
  public void setProjectType(String p_strProjectType)
  {
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.PROJECT_TYPE.getParameter(), p_strProjectType));
  }
  
  //(integer>0) Number of words in the source text. If missing or 0, will be calculated automatically.
  public void setWordCount(int p_iWordCount)
  {
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.WORD_COUNT.getParameter(), String.valueOf(p_iWordCount)));
  }
  
  // Any additional notes for the translator
  public void setNotes(String p_strNotes)
  {
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.NOTES.getParameter(), p_strNotes));
  }
  
  //  Callback URL to receive project-related notifications
  public void setCallBackUrl(String p_strCallbackUrl)
  {
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.CALLBACK_URL.getParameter(), p_strCallbackUrl));
  }
  
  public void setUserReference(String p_strUserReference)
  {
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.USER_REFERANCE.getParameter(), p_strUserReference));
  }
  public void setProjectReference(String p_strProjectReference)
  {
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.PROJECT_REFERANCE.getParameter(), p_strProjectReference));
  }
  
  public void setCustomField(int p_oIndex, String p_strCustomValue)
  {
    m_listOfQueryParams.add(new BasicNameValuePair(eQueryParameter.CUSTOM.getParameter() + p_oIndex, p_strCustomValue));
  }

  
  
  
  public  List<NameValuePair> getParameters()
  {
    return m_listOfQueryParams;
  }
}
